/*
 * @Author: fengxx
 * @Date: 2025-09-20 14:54:35
 * @LastEditors: fengxx
 * @LastEditTime: 2025-09-23 14:11:46
 */

import { defineStore } from 'pinia'
export const useProductStore = defineStore('product', {
  // 用来设置状态的默认值
  state: (): State => ({
    count: 4,
    name: 'Eduardo',
    userList: [],
    userDetail: null,
  }),
  getters: {
    // 根据state直接计算结果
    doubleCount: (state): number => state.count * 2,
    // 根据getter获取新的getter
    newCount(): number {
      return this.doubleCount + 1
    },
    // 返回一个函数，支持传参
    currentUser: (state) => {
      return (name: string) => state.userList.filter((item) => item.name == name)
    },
  },
  actions: {
    increment() {
      this.count++
    },
  },
})

interface State {
  count: number
  name: string
  userList: User[]
  userDetail: User | null
}
interface User {
  name: string
  age: number
}
